package com.tusuo.stds.ui.scenesetting;

import android.content.Intent;
import android.support.annotation.NonNull;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;

import com.hannesdorfmann.mosby.mvp.MvpPresenter;
import com.hannesdorfmann.mosby.mvp.MvpView;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadmoreListener;
import com.tusuo.stds.MyApp;
import com.tusuo.stds.R;
import com.tusuo.stds.di.DaggerScope;
import com.tusuo.stds.di.Injector;
import com.tusuo.stds.rest.ApiHelper;
import com.tusuo.stds.rest.SceneList;
import com.tusuo.stds.ui.BaseToolbarActivity;
import com.tusuo.stds.ui.ToolbarOwner;
import com.tusuo.stds.ui.main.SceneListAdapter;
import com.tusuo.stds.ui.scenedetail.SceneDetailActivity_;

import org.androidannotations.annotations.AfterInject;
import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.EActivity;
import org.androidannotations.annotations.ViewById;
import org.parceler.Parcels;

import java.util.ArrayList;
import java.util.List;

import javax.inject.Inject;

import autodagger.AutoComponent;
import autodagger.AutoInjector;
import rx.Subscriber;

/**
 * Created by developer on 17-8-29.
 */

@AutoComponent(
        dependencies = MyApp.class
)
@AutoInjector
@DaggerScope(NewOnlineActivity.class)
@EActivity(R.layout.activity_new_online)
public class NewOnlineActivity extends BaseToolbarActivity {

    @Inject
    ApiHelper mApiHelper;

    @ViewById(R.id.new_online_list)
    ListView mListView;
    @ViewById(R.id.new_online_refresh_layout)
    SmartRefreshLayout mRefreshLayout;

    private List<SceneList.Scene> mData;
    private List<SceneList.Scene> tempData;
    private int mTempIndex = 0;
    private SceneListAdapter adapter;

    @NonNull
    @Override
    public MvpPresenter createPresenter() {
        return new MvpPresenter() {
            @Override
            public void attachView(MvpView view) {
            }

            @Override
            public void detachView(boolean retainInstance) {
            }
        };
    }

    @AfterInject
    void initDI() {
        Injector.initNewOnlineActivityComponent().inject(this);
    }

    @AfterViews
    void initData() {
        setupUI(this);
        ToolbarOwner.Config config = new ToolbarOwner.Config("最新上线", true, null);
        mToolbarOwner.setConfig(config);

        mData = new ArrayList<>();
        mApiHelper.getSceneList(new GetNewOnlineSubscriber(),3,"");
        mListView.setOverScrollMode(View.OVER_SCROLL_NEVER);

        mRefreshLayout.setFooterHeight(0);

        mRefreshLayout.setEnableRefresh(false);

        mRefreshLayout.setOnLoadmoreListener(new OnLoadmoreListener() {
            @Override
            public void onLoadmore(RefreshLayout refreshlayout) {
                refreshlayout.finishLoadmore();
                loadMoreData();
            }
        });

        mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent intent = new Intent(NewOnlineActivity.this, SceneDetailActivity_.class);
                intent.putExtra(SceneList.Scene.EXTRA_NAME, Parcels.wrap(mData.get(position)));
                startActivity(intent);
            }
        });
    }

    private void loadMoreData() {
        int max = (tempData.size() - mData.size()) > 10 ? 10 : (tempData.size() - mData.size());

        for (int i = 0; i < max ; i ++) {
            mData.add(tempData.get(mTempIndex + i));
        }

        mTempIndex += max;

        if (adapter == null) {
            adapter = new SceneListAdapter(this, mData);
            mListView.setAdapter(adapter);
        }else {
            adapter.notifyDataSetChanged();
        }
    }

    private class GetNewOnlineSubscriber extends Subscriber<SceneList> {

        @Override
        public void onCompleted() {

        }

        @Override
        public void onError(Throwable e) {

        }

        @Override
        public void onNext(SceneList sceneList) {
            if (sceneList != null && sceneList.Total > 0) {
                tempData = sceneList.Data;
                loadMoreData();
            }
        }
    }






}
